<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" class="demo-form-inline">
      <el-form-item label="登录地址" prop="ipaddr">
        <el-input v-model="queryParams.ipaddr" placeholder="请输入登录地址" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable @keyup.enter="handleQuery" width="500" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" style="width: 100%" :data="list">
      <el-table-column label="序号" type="index" align="center" width="80" />
      <el-table-column label="会话编号" align="center" prop="tokenId" :show-overflow-tooltip="true" />
      <el-table-column label="登录名称" align="center" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="主机" align="center" prop="ipaddr" :show-overflow-tooltip="true" />
      <el-table-column label="登录地点" align="center" prop="loginLocation" :show-overflow-tooltip="true" />
      <el-table-column label="浏览器" align="center" prop="browser" />
      <el-table-column label="操作系统" align="center" prop="os" />
      <el-table-column label="登录时间" align="center" prop="loginTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button type="primary" link @click="handleForceLogout(scope.row)" v-p="['monitor:online:forceLogout']">
            强退
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @update:page="handlePageChange"
      @update:limit="handlePageSizeChange"
    />
  </div>
</template>

<script setup lang="ts">
import type { FormInstance } from "element-plus";
import { onMounted, ref } from "vue";
import { forceLogoutOnlineApi, getOnlineListApi } from "@/api";
import { ElMessage, ElMessageBox } from "element-plus";
import Pagination from "@/components/pagination/Pagination.vue";

const loading = ref(false);
const list = ref([]);
const total = ref(0);
const queryForm = ref<FormInstance>();
const queryParams = ref({
  ipaddr: undefined,
  userName: undefined,
  pageNum: 1,
  pageSize: 10
});

const handleQuery = () => {
  getOnlineList();
};

const resetQuery = () => {
  queryForm.value?.resetFields();
  getOnlineList();
};

const handleForceLogout = (row: any) => {
  ElMessageBox.confirm(`是否确认强退名称为${row.userName}的用户？`, "系统提示", {
    type: "warning"
  })
    .then(async () => {
      try {
        const res = await forceLogoutOnlineApi(row.tokenId);
        if (res.code !== 200) {
          return ElMessage.error(res.message);
        }
        ElMessage.success("强退成功");
        getOnlineList();
      } catch (error) {
        ElMessage.error("强退用户失败!");
      }
    })
    .catch(() => {});
};

const handlePageChange = (page: number) => {
  queryParams.value.pageNum = page;
};
const handlePageSizeChange = (size: number) => {
  queryParams.value.pageSize = size;
};

const getOnlineList = async () => {
  try {
    loading.value = true;
    const res = await getOnlineListApi(queryParams.value);
    if (res.code !== 200) {
      return ElMessage.error(res.message);
    }
    list.value = res.data;
    total.value = res.total as number;
  } catch (error) {
    ElMessage.error("网络异常，请稍后再试!");
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  getOnlineList();
});
</script>
<style lang="scss" scoped>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}
</style>
